package com.luobd.bpm.dto;

import cn.hutool.core.date.LocalDateTimeUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;

import java.time.LocalDateTime;

@Data
@ApiModel(description = "流程任务")
public class ProcessTaskDTO {


    @ApiModelProperty(value = "任务id")
    private String id;


    @ApiModelProperty(value = "流程标题")
    private String title;



    @ApiModelProperty(value = "任务名称")
    private String name;

    @ApiModelProperty(value = "任务类型")
    private String category;


    @ApiModelProperty(value = "流程发起人")
    private String owner;



    @ApiModelProperty(value = "流程实例id")
    private String processInstanceId;



    @ApiModelProperty(value = "流程定义id")
    private String processDefinitionId;


    @ApiModelProperty(value = "创建时间")
    private LocalDateTime createTime;


    @ApiModelProperty(value = "开始时间")
    private LocalDateTime startTime;


    @ApiModelProperty(value = "结束时间")
    private LocalDateTime endDateTime;




    @ApiModelProperty(value = "执行实例id")
    private String executionId;


    @ApiModelProperty(value = "任务办理人")
    private String assignee;


    @ApiModelProperty(value = "任务办理人名称")
    private String assigneeName;



    @ApiModelProperty(value = "表单key")
    private String formKey;


    @ApiModelProperty(value = "审批状态  complete  running  current")
    private String status;


    @ApiModelProperty(value = "任务定义key")
    private String taskDefKey;


    @ApiModelProperty(value = "发起人id")
    private String startUserId;




    public ProcessTaskDTO (){};





    public ProcessTaskDTO(Task task) {
        this.id = task.getId();
        this.name = task.getName();
        this.category = task.getCategory();
        this.processInstanceId = task.getProcessInstanceId();
        this.processDefinitionId = task.getProcessDefinitionId();
        this.executionId = task.getExecutionId();
        this.createTime = LocalDateTimeUtil.of(task.getCreateTime());
        this.assignee = task.getAssignee();
        this.owner = task.getOwner();
        this.taskDefKey = task.getTaskDefinitionKey();

    }


    public ProcessTaskDTO(HistoricTaskInstance task) {
        this.id = task.getId();
        this.name = task.getName();
        this.category = task.getCategory();
        this.processInstanceId = task.getProcessInstanceId();
        this.processDefinitionId = task.getProcessDefinitionId();
        this.executionId = task.getExecutionId();
        this.createTime = LocalDateTimeUtil.of(task.getCreateTime());
        this.assignee = task.getAssignee();
        this.owner = task.getOwner();
        this.taskDefKey = task.getTaskDefinitionKey();
        if(task.getStartTime() != null) {
            this.startTime = LocalDateTimeUtil.of(task.getStartTime());
        }
        if(task.getEndTime() != null) {
            this.endDateTime = LocalDateTimeUtil.of(task.getEndTime());
        }
        fillStatus();
    }


    private void fillStatus() {
        if(this.endDateTime != null) {
            this.status = "complete";
        }else if(this.startTime != null) {
            this.status = "current";
        }else {
            this.status = "running";
        }
    }




}
